package cn.kailangtu.commontools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.format.Formatter;
import android.util.Log;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class WifiUtils {

    private static WifiManager wifiManager;
    private static String TAG = "WifiUtils==>";
    public static boolean wifiIsConnection(Context context){
        if(wifiManager == null)
        {
            wifiManager=(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        }
        try
        {
            // we check if wifi is connected to any of the connections
            String ip= Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
            return true;

        }
        catch (Exception e)
        {
            // print error if unsuccessful to connect
            e.printStackTrace();
        }

        return false;
    }


    public static List<String> getAllIpAddresses(Context context) {
        List<String> ipAddresses = new ArrayList<>();

        // 检查是否为 Android 5.0 及以上版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                Network[] networks = connectivityManager.getAllNetworks();
                for (Network network : networks) {
                    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
                    if (capabilities != null) {
                        try {
                            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                            while (interfaces.hasMoreElements()) {
                                NetworkInterface networkInterface = interfaces.nextElement();
                                if (networkInterface.isUp()) {
                                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                                    while (addresses.hasMoreElements()) {
                                        InetAddress address = addresses.nextElement();
                                        ipAddresses.add(address.getHostAddress());
                                    }
                                }
                            }
                        } catch (Exception e) {
                            Log.e(TAG, "Error getting IP addresses: " + e.getMessage());
                        }
                    }
                }
            }
        } else {
            // Android 5.0 以下版本
            try {
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = interfaces.nextElement();
                    if (networkInterface.isUp()) {
                        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            InetAddress address = addresses.nextElement();
                            ipAddresses.add(address.getHostAddress());
                        }
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Error getting IP addresses: " + e.getMessage());
            }
        }

        return ipAddresses;
    }

    public static String getIpAddressFromWifi(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            return String.format("%d.%d.%d.%d",
                    (ipAddress & 0xff),
                    (ipAddress >> 8 & 0xff),
                    (ipAddress >> 16 & 0xff),
                    (ipAddress >> 24 & 0xff));
        }
        return null;
    }

    public static List<String> getIPv4Addresses(List<String> allAddresses) {
        List<String> ipv4Addresses = new ArrayList<>();
        for (String address : allAddresses) {
            try {
                InetAddress inetAddress = InetAddress.getByName(address);
                if (inetAddress instanceof Inet4Address) {
                    ipv4Addresses.add(address);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error filtering IPv4 addresses: " + e.getMessage());
            }
        }
        return ipv4Addresses;
    }

    public static List<String> getIPv6Addresses(List<String> allAddresses) {
        List<String> ipv6Addresses = new ArrayList<>();
        for (String address : allAddresses) {
            try {
                InetAddress inetAddress = InetAddress.getByName(address);
                if (!(inetAddress instanceof Inet4Address)) {
                    ipv6Addresses.add(address);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error filtering IPv6 addresses: " + e.getMessage());
            }
        }
        return ipv6Addresses;
    }

}
